package com.waiter.agent;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class TableListActivity extends Activity {
	private Context mContext;
	private Integer[] mItems = { 1, 2, 3, 4, 5, 6, 7, 8 };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tablelist);

		GridView g = (GridView) findViewById(R.id.table_grid);
		g.setAdapter(new ItemAdapter(this));
	}

	public class ItemAdapter extends BaseAdapter {
		public ItemAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mItems.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			FrameLayout item;
			if (convertView == null) {
				item = new FrameLayout(mContext);
				item.setLayoutParams(new GridView.LayoutParams(80, 80));

				ImageView img = new ImageView(mContext);
				img.setId(9998);
				img.setOnClickListener(ButtonListener());
				img.setLayoutParams(new FrameLayout.LayoutParams(
						FrameLayout.LayoutParams.MATCH_PARENT,
						FrameLayout.LayoutParams.MATCH_PARENT));

				TextView text = new TextView(mContext);
				text.setTypeface(null, Typeface.BOLD);
				text.setId(9999);
				text.setText(Integer.toString(mItems[position]));
				text.setTextSize(30);
				text.setLayoutParams(new FrameLayout.LayoutParams(
						FrameLayout.LayoutParams.WRAP_CONTENT,
						FrameLayout.LayoutParams.WRAP_CONTENT,
						Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL));

				if (position % 2 != 0) {
					img.setImageResource(R.drawable.plate_disabled);
					text.setTextColor(Color.GRAY);
				} else {
					img.setImageResource(R.drawable.plate);
					text.setTextColor(Color.RED);
				}

				img.setScaleType(ImageView.ScaleType.FIT_CENTER);
				item.addView(img);
				item.addView(text);
			} else {
				item = (FrameLayout) convertView;
			}

			return item;
		}

		public OnClickListener ButtonListener() {
			return new OnClickListener() {

				public void onClick(View v) {
					FrameLayout parent = (FrameLayout) v.getParent();
					TextView text = (TextView) parent.findViewById(9999);

					Intent i = new Intent(getApplicationContext(),
							BillHistoryActivity.class);
					startActivity(i);

				}
			};
		}
	}
}
